import { Component, OnInit } from '@angular/core';
import { SystemConfigService } from '@core/services/system-config.service';

@Component({
  selector: 'app-user-mag',
  templateUrl: './user-mag.component.html',
  styleUrls: ['./user-mag.component.less']
})
export class UserMagComponent implements OnInit {

  public total : any = 0;
  public pageIndex : any = 1;
  public pageSize : any = 10;
  public userInfos : any;
  public roleLists : any;
  public loginId : any;
  public roleId : any = '';
  public isVisible = false;
  public isConfirmLoading = false;
  public isVisible1 = false;
  public isConfirmLoading1 = false;
  public roleDetail : any = {
    roles: []
  };
  public password : any;
  public passWordId  : any;

  constructor(private systemService: SystemConfigService) { }

  //模态框
  showModal(id): void {
    this.isVisible = true;

    this.systemService.getRole().then((data: any) => {
      this.roleLists = data.data
    }, data => {
    });

    if(id == null||id == ''){
      this.roleId = '';
      this.roleDetail = {};
      this.roleDetail.roles = [];
    }else{
      this.systemService.userInfoById(id).then((data: any) => {
        this.roleDetail = data.data
      }, data => {
      });
    }
  }

  handleOk(): void {
    this.isConfirmLoading = true;
    this.rolListSavaData();
    let params = {
      id: this.roleDetail.id,
      loginId: this.roleDetail.loginId,
      remark: this.roleDetail.remark,
      roles: this.roleDetail.roles
    };
    console.log(params);
    this.systemService.userInfoUpdate(params).then((data: any) => {
      this.ngOnInit();
      this.isVisible = false;
      this.isConfirmLoading = false;
    }, data => {
    })
  }

  handleCancel(): void {
    this.isVisible = false;
    this.isVisible1 = false;
  }

  //格式化提交后台的角色数据
  rolListSavaData(){
    let array = [];
    let data = this.roleDetail.roles;
    for(let i=0;i<data.length;i++){
      array.push({
        id: data[i].id,
        menuIds: null,
        remark: null,
        name: data[i].name,
        sort: data[i].sort

      });
      array[array.length-1].isDefault = data[i].isDefault == true?1:(data[i].isDefault == false?0:data[i].isDefault);
    };
    this.roleDetail.roles = array;
  }

  //添加默认角色
  addRole(rId){
    if(rId == ''||rId == null){
      return
    }
    this.roleDetail.roles = [ ...this.roleDetail.roles, {
      id: this.roleId,
      isDefault: 0,
      name: '',
      sort: this.roleDetail.roles.length
    }];

    for(let i=0;i<this.roleLists.length;i++){
      if(rId == this.roleLists[i].id){
        this.roleDetail.roles[this.roleDetail.roles.length-1].name = this.roleLists[i].name
      }
    }
    console.log(this.roleDetail.roles)
  }

  //删除默认角色
  rDel(rId){
    this.roleDetail.roles = this.roleDetail.roles.filter(d => d.id !== rId);
  }

  //删除操作
  showDel(id){
    this.systemService.userInfoDel(id).then((data: any) => {
      if(this.total%10==1) {
        this.pageIndex--;
        if(this.pageIndex==0){
          this.pageIndex = 1;
        }
      }
      this.ngOnInit();
    }, data => {
    });
  }

  //修改密码模态框
  showModal1(id){
    this.isVisible1 = true;
    this.passWordId = id;
    this.password = '';
  }

  handleOk1(){
    let params = {
      id: this.passWordId,
      password: this.password
    };
    this.systemService.userInfoPass(params).then((data: any) => {
      this.ngOnInit();
      this.isVisible1 = false;
      this.isConfirmLoading1 = false;
    }, data => {
    })
  }

  search(){
    this.pageIndex = 1;
    this.loadPage();
  }

  loadPage(){
    this.ngOnInit();
  }

  ngOnInit() {
    let params = {
      pageNum: this.pageIndex,
      pageSize: this.pageSize,
      loginId: this.loginId == null?'':this.loginId
    };
    this.systemService.getUserInfo(params).then((data: any) => {
      this.userInfos = data.data;
      this.total = data.recordsTotal;
    }, data => {
    });
  }

}
